package com.baizhi.config;


import com.baizhi.filter.AccessControlAllowFilter;
import com.baizhi.intercepter.MyInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        //开启 count 的 join 优化,只针对部分 left join
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        // 设置请求的页面大于最大页后操作， true调回到首页， 默认false
        paginationInterceptor.setOverflow(true);
        // 设置最大单页限制数量，默认 500 条，-1 不受限制
        paginationInterceptor.setLimit(500);
        return paginationInterceptor;
    }
    @Bean
    public FilterRegistrationBean filterRegist() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter(new AccessControlAllowFilter());
        frBean.addUrlPatterns("/*");
        return frBean;
    }
        //拦截器
        public void addInterceptors(InterceptorRegistry registry) {
            //添加MyInterceptor到注册机里面，该拦截器拦截所有的请求，排除掉/user/*的请求
            registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**")
                    .excludePathPatterns("/user/**");

    }
}
